//
//  NSString+LKHTML.m
//  YuxiCloud
//
//  Created by LIUKUN-PC on 16/11/4.
//  Copyright © 2016年 ynnissi. All rights reserved.
//

#import "NSString+LKHTML.h"


@implementation NSString (LKHTML)

//去掉 HTML 字符串中的标签
- (NSString *)filterHTML {
    NSString *html = self;
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    //    NSString * regEx = @"<([^>]*)>";
    //    html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
    return html;
}

- (NSString *)stringByReplaySacpeHTMLTag {
    
    return [self stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@"  "];
   
}

- (NSString *)stringByReplayEnterHTMLTag {
    return [self stringByReplacingOccurrencesOfString:@"<br/>" withString:@"\n"];
}

#pragma mark - HTML -> AttributedString

- (NSAttributedString *)htmlConvertAttributedString {
    return [self htmlConvertAttributedStringWithFontSize:15];
}


- (NSAttributedString *)htmlConvertAttributedStringWithFontSize:(NSInteger)fontSize {
    return [self htmlConvertAttributedStringWithFontSize:fontSize color:nil];
}

//将 &lt 等类似的字符转化为HTML中的“<”等
- (NSString *)htmlEntityDecode {
    
    NSString *string = self;
    
    string = [string stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    string = [string stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"]; // Do this last so that, e.g. @"&amp;lt;" goes to @"&lt;" not @"<"
    string = [string stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@" "];
    return string;
}

- (NSAttributedString *)htmlConvertAttributedStringWithFontSize:(NSInteger)fontSize color:(UIColor *)color {
    NSData *data = [self dataUsingEncoding:NSUnicodeStringEncoding];
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    
    NSAttributedString *html = [[NSAttributedString alloc]initWithData:data options:options documentAttributes:nil error:nil];
    
    if (fontSize < 5 && color == nil) {
        return html;
    }
    
    NSMutableAttributedString *mHtml = [html mutableCopy];
    NSRange totalRange = (NSRange){0,[mHtml length]};

    if (fontSize >= 5) {
        //替换字体
        [mHtml enumerateAttribute:NSFontAttributeName inRange:totalRange options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id value, NSRange range, BOOL *stop) {
            UIFont* currentFont = value;
            UIFont *replacementFont = nil;
            
            if ([currentFont.fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location != NSNotFound) {
                replacementFont = [UIFont boldSystemFontOfSize:fontSize];
            } else {
                replacementFont = [UIFont systemFontOfSize:fontSize];
            }
            
            [mHtml removeAttribute:NSFontAttributeName range:range];
            [mHtml addAttribute:NSFontAttributeName value:replacementFont range:range];
        }];
    }

    if (color) {
        //替换颜色
        [mHtml enumerateAttribute:NSForegroundColorAttributeName inRange:totalRange options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id value, NSRange range, BOOL *stop) {
            
            [mHtml removeAttribute:NSForegroundColorAttributeName range:range];
            [mHtml addAttribute:NSForegroundColorAttributeName value:color range:range];
        }];
    }
    
    return [mHtml copy];
}

@end


@implementation NSAttributedString (LKHTML)

- (NSString *)htmlString {
    NSDictionary *tempDic = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
                              NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)};
    NSData *htmlData = [self dataFromRange:NSMakeRange(0, self.length)
                         documentAttributes:tempDic
                                      error:nil];
    return [[NSString alloc] initWithData:htmlData
                                 encoding:NSUTF8StringEncoding];
}



@end
